//页面加载viewport
Ext.onReady(function(){
	var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"加载中，请稍等..."});
	myMask.show();
	//先创建左侧panel容器
	var treePanel = Ext.create("Ext.panel.Panel",{
	        region: 'west',
	        collapsible: true,
	        title: '功能项',
	        autoScroll: true,
	        resizable : true,
	        width: 220,
	        html:"<div id='tree' style='width:100%;height:100%;'></div>"
//	        items:[tree]
	    });
	//创建主页tab标签与功能标签
	var indexTab = Ext.create("Ext.tab.Tab",{
				id:"indexTab",
				title: '首页',
				hidden: true,
				html: "<iframe id=\"content\" name=\"content\" scroll=\"auto\" frameborder=\"0\" src=\"\" style='width:100%;height:100%;' />"
			});
	//创建右侧TAB容器
	var tabPanel = Ext.create("Ext.tab.Panel",{
				        region: 'center',
//				        xtype: 'tabpanel', // TabPanel itself has no title
				        activeTab: 0,      // First tab active by default
				        items: indexTab
				    });
	Ext.create('Ext.container.Viewport', {
	    layout: 'border',
	    renderTo: Ext.getBody(),
	    items: [
//	    {
//	        region: 'north',
//	        html: '<h1 class="x-panel-header">欢迎来到本平台</h1>',
////	        autoHeight: true,
//	        height: 90,
//	        border: false,
//	        resizable : true,
//	        autoScroll: true,
//	        margins: '0 0 5 0'
//	    },
	    treePanel,tabPanel]
	});
	/* 创建左侧树 */
	//定义树的Model
	var proxy = Ext.create("Ext.data.proxy.Ajax",{
		//默认的model为实现Ext.data.NodeInterface的Model
		url:"manager/module.action",	//此url为根据用户ID查询初始模块
		reader:{
			type: "json",
			root: "beans",	//返回的Json格式根名称
			successProperty: "success"
		}
	});
	proxy.processResponse = function(success, operation, request, response, callback, scope){
		var json = eval("(" + response.responseText + ")");
		if(json.success == false) {
			Ext.get("body").setVisible(false, false);	//隐藏body
			alert("请登录后再进行相关操作！");
			if(Ext.isIE) {
				window.location.href="login.jsp";
			}else if(Ext.isChrome || Ext.isFF3_6) {
				window.location.href="manager/login.jsp";
			} else {
				window.location.href="manager/login.jsp";
			}
		}
		if (success === true) {
			me = this;
            reader = me.getReader();
            result = reader.read(me.extractResponseData(response));
            records = result.records;
//            length = records.length;//本行在IE中报错
            if (result.success !== false) {
                mc = Ext.create('Ext.util.MixedCollection', true, function(r) {return r.getId();});
                mc.addAll(operation.records);
                for (i = 0; i < records.length; i++) {
                    record = mc.get(records[i].getId());
                    
                    if (record) {
                        record.beginEdit();
                        record.set(record.data);
                        record.endEdit(true);
                    }
                }
                
                //see comment in buildRequest for why we include the response object here
                Ext.apply(operation, {
                    response: response,
                    resultSet: result
                });
                
                operation.setCompleted();
                operation.setSuccessful();
            } else {
                operation.setException(result.message);
                me.fireEvent('exception', this, response, operation);
            }
        } else {
            me.setException(operation, response);
            me.fireEvent('exception', this, response, operation);              
        }
            
        //this callback is the one that was passed to the 'read' or 'write' function above
        if (typeof callback == 'function') {
            callback.call(scope || me, operation);
        }
            
        me.afterRequest(request, success);
        
		//取消加载框
		myMask.hide();
	};
	//树数据存储
	var store = Ext.create('Ext.data.TreeStore', {
				    proxy: proxy
				});
	//左侧节点树
	var tree = Ext.create('Ext.tree.Panel', {
					renderTo: "tree",
				    store: store,
				    width: Ext.get("tree").getWidth(),
				    height:Ext.get("tree").getHeight(),
				    defaultRootId: 0,	//设置默认的根ID为0
				    rootVisible: false,
				    autoScroll:true,
				    border: 0
				});
	tree.on("beforeload",function(){
		proxy.url="manager/getModuleNode.action";
	});
	//树节点单击事件
	tree.on("itemclick",function(view,record,item,index,e){
		if(!record.isLeaf()) {
			return;
		} else {
			//发送ajax请求，改变右侧tabpanel所加载内容
			myMask.show();
			Ext.Ajax.timeout = 60000;
			Ext.Ajax.request({
			    url: 'manager/loadModule.action',
			    method: "post",
			    params: {
			        id: record.get("id")
			    },
			    timeout: 20000,
			    success: function(response){
			    	var json = eval("(" + response.responseText + ")");
			    	if(json.url == null || json.url == "") {
				    	myMask.hide();
				    	Ext.Msg.alert("提示","该节点未指定URL，设置请联系管理员！");
				    	return;
			    	} else {
			    		var url =  json.url + "?nodeId=" + record.get("id") + "&nodeText=" + record.get("text");
			    		url = encodeURI(url);
//				    	tabPanel.setActiveTab(funTab);
			    		document.getElementById("content").src = url;
			    	}
			    	myMask.hide();
			    },
			    failure: function(response) {
			    	myMask.hide();
			    	Ext.Msg.alert("提示","请求失败，请稍后重试！");
			    }
			});
		}
	});
	//给左侧的树容器，加上拖拽监听，能够动态改变树的宽高
	treePanel.on("resize",function(){
		tree.setWidth(Ext.get("tree").getWidth());
		tree.setHeight(Ext.get("tree").getHeight());
	});
});